package com.tebiecloud.act.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.act.client.constants.ResultEnum;
import com.tebiecloud.act.client.model.entity.ActCollect;
import com.tebiecloud.act.server.service.ActCollectService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 收藏表 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/actcollect")
public class ActCollectController {

    @Autowired
    private ActCollectService actCollectService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<ActCollect>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        ActCollect query = pageParams.mapToObject(ActCollect.class);
        QueryWrapper<ActCollect> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(actCollectService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<ActCollect> get(@RequestParam("id") Long id) {
        ActCollect entity = actCollectService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param actId 活动id,
     * @param userId 用户id
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "actId", required = false) Long actId,
        @RequestParam(value = "userId", required = false) Long userId
    ) {
        ActCollect entity = new ActCollect();
        entity.setActId(actId);
        entity.setUserId(userId);
        actCollectService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param actId 活动id
     * @param isCollect 是否收藏：0否，1是
     * @return
     */
    @PostMapping("creareorupdate")
    public ResultBody update(
            @RequestParam(value = "actId") Long actId,
            @RequestParam(value = "isCollect") Integer isCollect) {

        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        if(securityUserDetails==null){
            return ResultBodyUtils.failed(ResultEnum.ACT_IMAGE_UPLOAD_FAILED.getMessage());
        }
        actCollectService.creareOrUpdate(securityUserDetails.getUserId(),actId,isCollect);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            actCollectService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            actCollectService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
